package com.goose.bmm.controler.activity;

import java.util.ArrayList;
import java.util.Locale;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.goose.bmm.R;
import com.goose.bmm.model.business.DataGlobal;
import com.goose.bmm.model.entity.Event;
import com.goose.bmm.model.entity.History;
import com.goose.bmm.model.entity.Match;
import com.goose.bmm.model.entity.Player;
import com.goose.bmm.model.entity.Team;

public class MainMenuActivity extends ActionBarActivity implements
		OnClickListener {

	private static final int ID_DIALOG = 0;
	private static final int ENGLISH_LANGUAGE = 0;
	private static final int VIETNAMESE_LANGUAGE = 1;
	private static int CHOOSE_LANGUAGE;

	private Button mButtonNewMatch;
	private Button mButtonQuickMatch;
	private Button mButtonOptionsLanguage;

	private String[] options_language;
	private Locale myLocale;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().hide();
		setContentView(R.layout.activity_main_menu);

		mButtonNewMatch = (Button) findViewById(R.id.btn_new_match);
		mButtonQuickMatch = (Button) findViewById(R.id.btn_quick_match);
		mButtonOptionsLanguage = (Button) findViewById(R.id.btn_options_language);

		mButtonNewMatch.setOnClickListener(this);
		mButtonQuickMatch.setOnClickListener(this);
		mButtonOptionsLanguage.setOnClickListener(this);

		options_language = new String[] {
				getResources().getString(R.string.dialog_item_english),
				getResources().getString(R.string.dialog_item_vietnam) };

		resetAllDataGlobal();
	}

	private void resetAllDataGlobal() {
		DataGlobal.historyGlobal = new History();
		DataGlobal.matchGlobal = new Match();
		DataGlobal.homeTeamGlobal = new Team();
		DataGlobal.awayTeamGlobal = new Team();
		DataGlobal.homeListPlayer = new ArrayList<Player>();
		DataGlobal.guestListPlayer = new ArrayList<Player>();
		DataGlobal.listEvent = new ArrayList<Event>();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_new_match) {
			Intent intentSettingMatchActivity = new Intent(
					MainMenuActivity.this, SettingTotalActivity.class);
			startActivity(intentSettingMatchActivity);
			return;
		}
		if (v.getId() == R.id.btn_quick_match) {
			return;
		}
		if (v.getId() == R.id.btn_options_language) {
			showDialog(ID_DIALOG);
			return;
		}

	}

	private void changeLanguage(int options) {
		switch (options) {
		case ENGLISH_LANGUAGE: {
			myLocale = new Locale("");
			Resources res = getResources();
			DisplayMetrics dm = res.getDisplayMetrics();
			Configuration conf = res.getConfiguration();
			conf.locale = myLocale;
			res.updateConfiguration(conf, dm);
			Intent refresh = new Intent(this, MainMenuActivity.class);
			startActivity(refresh);
			finish();
			break;
		}
		case VIETNAMESE_LANGUAGE: {
			myLocale = new Locale("vn");
			Resources res = getResources();
			DisplayMetrics dm = res.getDisplayMetrics();
			Configuration conf = res.getConfiguration();
			conf.locale = myLocale;
			res.updateConfiguration(conf, dm);
			Intent refresh = new Intent(this, MainMenuActivity.class);
			startActivity(refresh);
			finish();
			break;
		}
		}
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_menu_more)
				.setTitle(
						getResources().getString(
								R.string.dialog_title_options_language))
				.setSingleChoiceItems(options_language, -1,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int index_language) {
								CHOOSE_LANGUAGE = index_language;
								changeLanguage(index_language);
								dialog.dismiss();
							}
						});
		AlertDialog optionsLanguageAlertDialog = builder.create();
		return optionsLanguageAlertDialog;
	}

	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
		AlertDialog prepare_radio_dialog = (AlertDialog) dialog;
		ListView list_radio = prepare_radio_dialog.getListView();
		for (int i = 0; i < list_radio.getCount(); i++) {
			list_radio.setItemChecked(i, false);
		}
		list_radio.setItemChecked(CHOOSE_LANGUAGE, true); // setting default
															// english
	}

	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this)
				.setMessage(
						getResources().getString(R.string.dialog_message_exit))
				.setPositiveButton(getResources().getString(R.string.yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}

						})
				.setNegativeButton(getResources().getString(R.string.no), null)
				.show();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		resetAllDataGlobal();
	}
}
